﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for UserProfilesDAL
/// </summary>
public class UserProfilesDAL: StandardConnectDB, IUserProfiles
{
	public UserProfilesDAL()
    {
	}


    public List<UserProfiles> FindUserWithByName(String username)
    {
        List<UserProfiles> listup = new List<UserProfiles>();

        Dictionary<String, Object> paramist = new Dictionary<String, Object>();
        paramist.Add("@pUserName", username);

        DataTable dt = this.DTExecuteSelect("spFindUserWithByName", paramist);
        foreach (DataRow dr in dt.Rows)
        {

            listup.Add(this.ConvertObjectDataRowToUserProfiles(dr));
        }
        return listup;
    }
    public List<UserProfiles> GetFriendUser(Users username)
    {
        List<UserProfiles> Listfriends = new List<UserProfiles>();

        Dictionary<String, Object> paramist = new Dictionary<String, Object>();

        paramist.Add("@pUsername", username.Username);

        DataTable dt = this.DTExecuteSelect("spGetFriendsofUser", paramist);
        foreach (DataRow dr in dt.Rows)
        {
            Listfriends.Add(this.ConvertObjectDataRowToUserProfiles(dr));
        }

        return Listfriends;

    }

    public UserProfiles GetUserProfileOfUser(Users user)
    {
        //Create object listUsersP
        UserProfiles userP = new UserProfiles();

        //Create object paramList store parameter execute store procedure
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();

        //Add parameter pIdRole to paramList
        paramList.Add("@pUsername", user.Username);

        //Get Users by store procedure
        DataTable dt = this.DTExecuteSelect("spGetUserProfilesByUsername", paramList);

        if (dt.Rows.Count == 1)
        {
            //Convert objec DataRow to object UserProfiles
            userP = this.ConvertObjectDataRowToUserProfiles(dt.Rows[0]);

        }
       
        return userP;
    }

    public bool CreateUserProfile(Users user, string name, bool gender, DateTime datebirth, string city, string people, string avata, string interests, string jobs,DateTime Createtime)
    {
        // Create Object uo

        // Create object paramlist strore parameter update store procedure
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();
        // 
        paramList.Add("@pUsername", user.Username);
        paramList.Add("@pNames", name);
        paramList.Add("@pGender", gender);
        paramList.Add("@pDateBirth", datebirth);
        paramList.Add("@pCity",city);
        paramList.Add("@pPeople", people);
        paramList.Add("@pAvatar", avata);
        paramList.Add("@pInterests", interests);
        paramList.Add("@pJobs", jobs);
        paramList.Add("@pCreatetime", Createtime);

    
        bool result = this.ExecuteUpdate("spCreateUserProfile", paramList);

         return result;

        
    }

    public bool UpdateUserProfile(UserProfiles userProfile)
    {
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();

        paramList.Add("@pUserName", userProfile.User.Username);
        paramList.Add("@pInterest", userProfile.Interests);
        paramList.Add("@pJobs", userProfile.Jobs);
        bool kq = this.ExecuteUpdate("spUpdateUserProfiles", paramList);
            
        return kq;

    }

    public bool UpdateAvatarUserProfiles(UserProfiles userProfiles)
    {
        bool kq = false;
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();
        paramList.Add("@pUserName", userProfiles.User.Username);
        paramList.Add("@pAvatar", userProfiles.Avatar);
        kq = this.ExecuteUpdate("spUpdateAvatarUserProfiles", paramList);
        return kq;
    }


    public UserProfiles ConvertObjectDataRowToUserProfiles(DataRow dr)
    {
        UserProfiles userprofile = new UserProfiles();

        userprofile.IdUserProfiles = Convert.ToInt32(dr["IdUserProfiles"].ToString());

        string username = dr["Names"].ToString();
        IUsers iUsers = new UsersDAL();
        userprofile.User = iUsers.GetUserByUsername(username);

        userprofile.Gender = Convert.ToBoolean(dr["Gender"].ToString());
     
     //   userprofile.DateBirth =DateTime.Parse(dr["DateBirth"].ToString());
        //  userprofile.DateBirth = Convert.ToDateTime(dr["DateBirth"].ToString());
        userprofile.City = dr["City"].ToString();
        userprofile.People = dr["People"].ToString();
        userprofile.Avatar = dr["Avatar"].ToString();
        userprofile.Interests = dr["Interests"].ToString();
        userprofile.Jobs = dr["Jobs"].ToString();
      //  userprofile.CreateTime = Convert.ToDateTime(dr["CreateTime"].ToString());
        
        return userprofile;
    }

   
    
}